//
//  LaunchBannerView.m
//  Friendsback
//
//  Created by 宋质彬 on 2018/3/9.
//  Copyright © 2018年 宋质彬. All rights reserved.
//

#import "LaunchBannerView.h"
#import "AppDelegate.h"

@interface LaunchBannerView ()<UIGestureRecognizerDelegate>

@property (copy, nonatomic) NSString * openURL;
@property (weak, nonatomic) UIImageView *imageView ;
@property (strong, nonatomic) NSTimer * timer;
@property (nonatomic,weak) UIButton *btn;
@property (nonatomic,strong) NSDictionary *infoDict;
@property (nonatomic , assign) NSInteger timeCount;

@end
@implementation LaunchBannerView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.timeCount = 2;
        //添加原创控件的所有子控件
        [self setUpAllChildView];
        
    }
    return self;
}
/**
 
 {
 id = 9;
 imgIndex = 1;
 imgUrl = "/files/2017-07-07/4d864fce-9831-47ee-a266-a8e365daf9ec.jpg";
 linkUrl = "http://www.weather.com.cn/weather/101020100.shtml";
 name = asd;
 }
 */
- (void)configLaunchBannerData:(NSArray *)splashSyjAdvertising{
    
    if ([JSLKitFactory isNotNull:splashSyjAdvertising] || splashSyjAdvertising.count != 0) {
        
        self.infoDict = [splashSyjAdvertising firstObject];
        
        NSDictionary * dict = [splashSyjAdvertising firstObject];
        NSURL *url =  [NSURL URLWithString:[NSString stringWithFormat:@"%@%@",Url_Server,dict[@"imgUrl"]]];
        
        
        
        NSString *imageName = nil;
        if (iPhone4) {
            imageName = @"start-640x960";
        }else if (iPhone5) {
            imageName = @"start-640x1136";
        }else if (iphone6) {
            imageName = @"start-750x1334";
        }else if(iphone6Plus){
            imageName = @"start-1242x2208";
        }else if (iPhoneX){
            imageName = @"start-1125x2436";
        }
        UIImage * img = [UIImage imageNamed:imageName];
        if (!img) {
            img = [UIImage imageNamed:@"start-1125x2436"];
        }
        [self.imageView sd_setImageWithURL:url placeholderImage:img];
    }
    
    
}

#pragma mark - 所有子控件
- (void)setUpAllChildView{
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0,SCREEN_WIDTH, SCREEN_HEIGHT)];
    imageView.contentMode = UIViewContentModeScaleAspectFit;
    imageView.tag = 111;
    NSURL *url =  [NSURL URLWithString:[NSString stringWithFormat:@"%@%@",Url_Server,@"/files/2017-07-03/87ea132a-a170-4600-aad5-70c4bbaacac5.png"]];
    
    NSString *imageName = nil;
    if (iPhone4) {
        imageName = @"start-640x960";
    }else if (iPhone5) {
        imageName = @"start-640x1136";
    }else if (iphone6) {
        imageName = @"start-750x1334";
    }else if(iphone6Plus){
        imageName = @"start-1242x2208";
    }else if (iPhoneX){
        imageName = @"start-1125x2436";
    }
    [imageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:imageName]];
    [self addSubview:imageView];
    self.imageView = imageView;
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(SCREEN_WIDTH - 44*AutoSizeScaleXX-15*AutoSizeScaleXX, 30, 44*AutoSizeScaleXX, 44*AutoSizeScaleXX);
    btn.backgroundColor = RGBACOLOR(0, 0, 0, 0.4);
    btn.titleLabel.font = [UIFont systemFontOfSize:20];
    btn.layer.cornerRadius = 8;
    btn.hidden = NO;
    btn.layer.masksToBounds = YES;
    btn.tag = 222;
    [btn setTitle:@"2s" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(tapAction) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:btn];
    btn.layer.mask =
    //    [JSLKitFactory roundedRect:btn.bounds roundingCorners:UIRectCornerAllCorners cornerRadius:0];
    [JSLKitFactory roundedRect:btn.bounds roundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(44*AutoSizeScaleXX*0.5, 44*AutoSizeScaleXX*0.5)];
    self.btn = btn;
    
    
    _timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timeOut) userInfo:nil repeats:YES];
    
    
    
    UITapGestureRecognizer * tapGestureReg = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapBannerAction)];
    tapGestureReg.delegate = self;
    imageView.userInteractionEnabled = YES;
    [imageView addGestureRecognizer:tapGestureReg];
    
    //    self.nowApplyButton = [JSLKitFactory jsl_createButtonWithFrame:CGRectZero superView:self.footerBackView setImage:nil setBackImage:nil font:LIGHT_FONT(17) titleColor:[UIColor whiteColor] backgroundColor:[@"#5594ff" hexStringToColor] title:@"立即申请" target:self action:@selector(buttonClick:)];
}


#pragma mark - Actions
- (void)tapAction {
    
    if (self.timer) {
        [self.timer invalidate];
        self.timer = nil;
    }
    
    [self tapActionWithSuccess:^() {
        if ([self.bannerViewDelegate respondsToSelector:@selector(comeToAddViewControllerWithUrl:isTimeOut:)]) {
            
            [self.bannerViewDelegate comeToAddViewControllerWithUrl:self.infoDict isTimeOut:YES];
            
        }
        
    }];
    
}

- (void)timeOut {
    NSLog(@"%ld",(long)self.timeCount);
    if (self.timeCount>0) {
        self.timeCount --;
        
        [self.btn setTitle:[NSString stringWithFormat:@"%lds",(long)self.timeCount] forState:UIControlStateNormal];
    }else{
        
        if (self.timer) {
            [self.timer invalidate];
            self.timer = nil;
        }
        
        [self tapActionWithSuccess:^() {
            if ([self.bannerViewDelegate respondsToSelector:@selector(comeToAddViewControllerWithUrl:isTimeOut:)]) {
                
                [self.bannerViewDelegate comeToAddViewControllerWithUrl:self.infoDict isTimeOut:YES];
                
            }
            
        }];
    }
    
}

- (void)tapBannerAction{
    
    if (self.timer) {
        [self.timer invalidate];
        self.timer = nil;
    }
    
    [self tapActionWithSuccess:^() {
        if ([self.bannerViewDelegate respondsToSelector:@selector(comeToAddViewControllerWithUrl:isTimeOut:)]) {
            
            [self.bannerViewDelegate comeToAddViewControllerWithUrl:self.infoDict isTimeOut:NO];
            
        }
        
    }];
}
#pragma mark - Memory Manage
- (void)tapActionWithSuccess:(void(^)(void))success
{
    
    [UIView animateWithDuration:1 animations:^{
        self.transform  = CGAffineTransformMakeScale(1.3, 1.3);
        self.alpha      = 0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
        
        if (success) {
            success();
        }
    }];
}

- (void)dealloc {
    if (self.timer) {
        [self.timer invalidate];
        self.timer = nil;
    }
}


@end
